package StudyCode.summary.myfile;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FileCreateShareFileLock {
	public static void main(String[] args) throws IOException {
		File file = new File("E:\\迅雷下载\\test1.txt");
		FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

		FileLock lock = channel.lock(0, Long.MAX_VALUE, true);

		try {
			lock = channel.tryLock(0, Long.MAX_VALUE, true);
		} catch (OverlappingFileLockException e) {
		}

		boolean isShared = lock.isShared();
		System.out.println("是否共享了文件锁？" + isShared);
		lock.release();

		channel.close();

	}
}
